clc
clear

ncfile = '../run/hope_output.n45.nc';

% var_name  = 'z';
% cmin = 0;
% cmax = 40000;
% LevelStep = 400;

% var_name  = 'us';
% cmin = -80;
% cmax = 80;
% LevelStep = 4;

% var_name  = 'vs';
% cmin = -40;
% cmax = 40;
% LevelStep = 4;

% var_name  = 'w';
% cmin = -1;
% cmax = 1;
% LevelStep = 0.05;

var_name  = 'p';
cmin = 0;
cmax = 100000;
LevelStep = 5000;

% var_name  = 'rho';
% cmin = 0;
% cmax = 1.4;

% var_name  = 't';
% cmin = 180;
% cmax = 300;

% var_name  = 'theta';
% cmin = 200;
% cmax = 1200;
% LevelStep = 40;

% var_name  = 'mr';
% cmin = 0;
% cmax = 0.02;
% LevelStep = 0.001;

plot_case = 'j'; % Choose from 'i' or 'j'
i         = 34;
j         = 23;
idom      = 4;
it        = 1;

FontSize = 10;

lon = ncread(ncfile,'lon');
lat = ncread(ncfile,'lat');
p   = ncread(ncfile,'p',[1,1,1,1,it],[Inf,Inf,Inf,Inf,1]);
z   = ncread(ncfile,'z',[1,1,1,1,1],[Inf,Inf,Inf,Inf,Inf]);
if(~strcmp(var_name,'z'))
    var = ncread(ncfile,var_name,[1,1,1,1,it],[Inf,Inf,Inf,Inf,1]);
else
    var = ncread(ncfile,var_name,[1,1,1,1,1],[Inf,Inf,Inf,Inf,Inf]);
end

nz = size(z,4);

if idom==1
    lon(lon>180) = lon(lon>180) - 360;
end

if strcmp(plot_case,'i')
    % i case
    lon_plt = repmat(squeeze(lon(:,j,idom)),1,nz);
    z_plt   = squeeze(z(1,:,j,:,idom));
    
    if strcmp(var_name,'z')
        var_plt = squeeze(var(1,:,j,:,idom));
        z_plt = squeeze(p(:,j,:,idom));
    else
        var_plt = squeeze(var(:,j,:,idom));
    end
    
    figure
    plt = pcolor(lon_plt,z_plt,var_plt);
    set(gca,'Clim',[cmin,cmax])
    shading interp
    colormap(jet)
    % colorbar
    hold on
    [plt,h] = contour(lon_plt,z_plt,var_plt,'k--','ShowText','on','LevelStep',LevelStep);
    clabel(plt,h,'FontSize',FontSize)
    title([var_name,' at ',num2str(it)])
    
elseif strcmp(plot_case,'j')
    % j case
    lat_plt = repmat(squeeze(lat(i,:,idom))',1,nz);
    z_plt   = squeeze(z(1,i,:,:,idom));
    
    if strcmp(var_name,'z')
        var_plt = squeeze(var(1,i,:,:,idom));
        z_plt   = squeeze(p(i,:,:,idom));
    else
        var_plt = squeeze(var(i,:,:,idom));
    end
    
    figure
    plt = pcolor(lat_plt,z_plt,var_plt);
    set(gca,'Clim',[cmin,cmax])
%     shading interp
    colormap(jet)
    % colorbar
    hold on
    [plt,h] = contour(lat_plt,z_plt,var_plt,'k--','ShowText','on','LevelStep',LevelStep);
    clabel(plt,h,'FontSize',FontSize)
    title([var_name,' at ',num2str(it)])
end